import { FormSchema } from '@/components/Form';
import { useDictStore } from '@/store/modules/dict';
import { validate } from '@/utils/validateUtil';
import { cloneDeep } from 'lodash-es';
import { ref } from 'vue';

export class State {
  public id = 0; // ID
  public name = ''; // 门店名称
  public address = ''; // 门店地址
  public lat = null; // 纬度
  public lng = null; // 经度
  public adcode = 0; // 地区编码
  public phone = ''; // 门店电话
  public businessId = 0; // 商户ID
  public createdAt = ''; // 创建时间
  public updatedAt = ''; // 更新时间
  // public businessDay = [1, 2, 3, 4, 5]; // 营业星期
  // public businessTimeStart = 3600000; // 营业时间-开始
  // public businessTimeEnd = 36000000; // 营业时间-结束
  public publicityImgs = []; // 门店图片

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  name: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入门店名称',
  },
  phone: {
    required: false,
    trigger: ['blur', 'input'],
    type: 'string',
    validator: validate.phone,
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'name',
    component: 'NInput',
    label: '门店名称',
    componentProps: {
      placeholder: '请输入门店名称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'phone',
    component: 'NInput',
    label: '门店电话',
    componentProps: {
      placeholder: '请输入门店电话',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'ID',
    key: 'id',
    align: 'left',
    width: 100,
  },
  {
    title: '门店名称',
    key: 'name',
    align: 'left',
    width: 300,
  },
  {
    title: '门店地址',
    key: 'address',
    align: 'left',
    width: -1,
  },
  {
    title: '门店电话',
    key: 'phone',
    align: 'left',
    width: 150,
  },
  {
    title: '账户余额（元）',
    key: 'balance',
    align: 'left',
    width: 150,
  },
  {
    title: '关联商户',
    key: 'adminMemberStoreName',
    align: 'left',
    width: 150,
  },
  {
    title: '创建时间',
    key: 'createdAt',
    align: 'left',
    width: 170,
  },
];

const dict = useDictStore();

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['xxb_md_yyzt']);
}